package com.itheima.spring_demo.controller;


import com.itheima.spring_demo.service.UserService;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

public class UserController {

    public static void main(String[] args) {
        // 1.创建spring的容器对象
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 2.从容器对象中获取userService对象
        UserService userService = applicationContext.getBean("userService", UserService.class);
        // 3.调用userService的方法进行业务逻辑处理
        userService.add();
    }

    @Test
    public void test1() {
        // BeanFactory属于延时加载
        BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
        UserService userService = beanFactory.getBean("userService", UserService.class);
        userService.add();
    }

    @Test
    public void test2() {
        // ApplicationContext属于非延时加载
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = applicationContext.getBean("userService", UserService.class);
        userService.add();
    }
}
